import { ref } from 'vue'
import type { FollowType } from '@/types/consult'
import { followDoctor } from '@/api/consult'
import { showSuccessToast } from 'vant'
/**
 * 全局复用自定义hooks
 * 抽离技巧
 * 1相同部分直接拷贝
 * 2不同部分以函数参数形式传入
 */
export const useFollow = (type: FollowType = 'doc') => {
    // 关注逻辑
    const loading = ref(false)
    const follow = async (doc: { id: string; likeFlag: 0 | 1 }) => {
        // 防止重复点击
        loading.value = true
        try {
            let res = await followDoctor(doc.id, type)
            doc.likeFlag = doc.likeFlag === 1 ? 0 : 1
            showSuccessToast(doc.likeFlag === 1 ? "关注成功" : "取关成功")
        } finally {
            // 无论 try / catch 结果如何都会执行的代码块
            loading.value = false
        }
    }
    return { loading, follow }
}